package com.itheima.service.impl;
/*
    ヾ(❀ﾟ▽ﾟ)ノ 
 
 Grey1996year@163.com 
 
*/

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

@Service(interfaceClass = CheckGroupService.class,protocol = "dubbo")
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

	@Autowired
	private CheckGroupMapper checkGroupMapper;

	@Override
	public void add(CheckGroup checkGroup,Integer[] checkitemIds) {
		//先插入t_checkGroup表
		checkGroupMapper.add(checkGroup);

		//取出插入checkgroup时自增的主键id
		Integer id = checkGroup.getId();

		//在对中间表进行插入操作
		for (Integer checkitemId : checkitemIds) {
			HashMap<String,Integer> map = new HashMap<>();
			map.put("checkgroupId",id);
			map.put("checkitemId",checkitemId);

			checkGroupMapper.addRelate(map);
		}


	}

	@Override
	public PageResult findPage(QueryPageBean queryPageBean) {
		//取出参数
		Integer currentPage = queryPageBean.getCurrentPage();
		Integer pageSize = queryPageBean.getPageSize();
		String queryString = queryPageBean.getQueryString();
		//分页助手
		PageHelper.startPage(currentPage, pageSize);
		//条件查询
		Page<CheckGroup> page = checkGroupMapper.selectByCondition(queryString);

		return new PageResult(page.getTotal(),page.getResult());
	}

	@Override
	public CheckGroup findgroupById(Integer id) {
		CheckGroup checkGroup = checkGroupMapper.findgroupById(id);

		return checkGroup;
	}

	@Override
	public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
		/*
			修改中间表
			1.先删除中间表的数据;
			2.再添加数据
		 */
		checkGroupMapper.deleteRelate(checkGroup.getId());

		for (Integer checkitemId : checkitemIds) {
			HashMap<String,Integer> map = new HashMap<>();
			map.put("checkgroupId",checkGroup.getId());
			map.put("checkitemId",checkitemId);
			checkGroupMapper.addRelate(map);
		}

		//修改checkgroup
		checkGroupMapper.edit(checkGroup);

	}

	@Override
	public void delete(Integer id) {
		//先删除中间表
		checkGroupMapper.deleteRelate(id);
		//再删除checkGroup表
		checkGroupMapper.delete(id);
	}

	@Override
	public List<CheckGroup> findAll() {
		List<CheckGroup> checkGroupList = checkGroupMapper.findAll();


		return checkGroupList;
	}
}
